package org.htmlparser.beans;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.PrintStream;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Vector;
import org.htmlparser.Parser;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.EncodingChangeException;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

public class LinkBean
  implements Serializable
{
  public static final String PROP_LINKS_PROPERTY = "links";
  public static final String PROP_URL_PROPERTY = "URL";
  static Class class$org$htmlparser$tags$LinkTag;
  protected URL[] mLinks;
  protected Parser mParser;
  protected PropertyChangeSupport mPropertySupport;

  public LinkBean()
  {
    this.mPropertySupport = new PropertyChangeSupport(this);
    this.mLinks = null;
    this.mParser = new Parser();
  }

  static Class class$(String paramString)
  {
    Class localClass;
    try
    {
      localClass = Class.forName(paramString);
      return localClass;
    }
    catch (ClassNotFoundException localClassNotFoundException)
    {
      throw new NoClassDefFoundError().initCause(localClassNotFoundException);
    }
  }

  public static void main(String[] paramArrayOfString)
  {
    if (paramArrayOfString.length <= 0)
    {
      System.out.println("Usage: java -classpath htmlparser.jar org.htmlparser.beans.LinkBean <http://whatever_url>");
      return;
    }
    LinkBean localLinkBean = new LinkBean();
    localLinkBean.setURL(paramArrayOfString[0]);
    URL[] arrayOfURL = localLinkBean.getLinks();
    int i = 0;
    while (true)
    {
      if (i >= arrayOfURL.length);
      System.out.println(arrayOfURL[i]);
      ++i;
    }
  }

  private void setLinks()
  {
    if (getURL() != null);
    try
    {
      URL[] arrayOfURL1 = extractLinks();
      if (!(equivalent(this.mLinks, arrayOfURL1)))
      {
        URL[] arrayOfURL2 = this.mLinks;
        this.mLinks = arrayOfURL1;
        this.mPropertySupport.firePropertyChange("links", arrayOfURL2, this.mLinks);
      }
      return;
    }
    catch (ParserException localParserException)
    {
      this.mLinks = null;
    }
  }

  public void addPropertyChangeListener(PropertyChangeListener paramPropertyChangeListener)
  {
    this.mPropertySupport.addPropertyChangeListener(paramPropertyChangeListener);
  }

  protected boolean equivalent(URL[] paramArrayOfURL1, URL[] paramArrayOfURL2)
  {
    int i = 0;
    if ((paramArrayOfURL1 == null) && (paramArrayOfURL2 == null))
      i = 1;
    do
      return i;
    while ((paramArrayOfURL1 == null) || (paramArrayOfURL2 == null) || (paramArrayOfURL1.length != paramArrayOfURL2.length));
    i = 1;
    int j = 0;
    while (true)
    {
      if ((j >= paramArrayOfURL1.length) || (i == 0));
      if (paramArrayOfURL1[j] != paramArrayOfURL2[j])
        i = 0;
      ++j;
    }
  }

  protected URL[] extractLinks()
    throws ParserException
  {
    Class localClass;
    NodeClassFilter localNodeClassFilter;
    NodeList localNodeList1;
    Vector localVector;
    int i;
    this.mParser.reset();
    if (class$org$htmlparser$tags$LinkTag == null)
    {
      localClass = class$("org.htmlparser.tags.LinkTag");
      class$org$htmlparser$tags$LinkTag = localClass;
      localNodeClassFilter = new NodeClassFilter(localClass);
    }
    try
    {
      NodeList localNodeList2 = this.mParser.extractAllNodesThatMatch(localNodeClassFilter);
      localNodeList1 = localNodeList2;
      localVector = new Vector();
      i = 0;
      if (i < localNodeList1.size());
    }
    catch (EncodingChangeException localEncodingChangeException)
    {
      try
      {
        while (true)
        {
          while (true)
          {
            while (true)
            {
              localVector.add(new URL(((LinkTag)localNodeList1.elementAt(i)).getLink()));
              ++i;
            }
            localClass = class$org$htmlparser$tags$LinkTag;
          }
          localEncodingChangeException = localEncodingChangeException;
          this.mParser.reset();
          localNodeList1 = this.mParser.extractAllNodesThatMatch(localNodeClassFilter);
        }
        URL[] arrayOfURL = new URL[localVector.size()];
        localVector.copyInto(arrayOfURL);
        return arrayOfURL;
      }
      catch (MalformedURLException localMalformedURLException)
      {
      }
    }
  }

  public URLConnection getConnection()
  {
    return this.mParser.getConnection();
  }

  public URL[] getLinks()
  {
    if (this.mLinks == null);
    try
    {
      this.mLinks = extractLinks();
      this.mPropertySupport.firePropertyChange("links", null, this.mLinks);
      return this.mLinks;
    }
    catch (ParserException localParserException)
    {
      this.mLinks = null;
    }
  }

  public String getURL()
  {
    return this.mParser.getURL();
  }

  public void removePropertyChangeListener(PropertyChangeListener paramPropertyChangeListener)
  {
    this.mPropertySupport.removePropertyChangeListener(paramPropertyChangeListener);
  }

  public void setConnection(URLConnection paramURLConnection)
  {
    try
    {
      this.mParser.setConnection(paramURLConnection);
      setLinks();
      return;
    }
    catch (ParserException localParserException)
    {
    }
  }

  public void setURL(String paramString)
  {
    String str = getURL();
    if (((str == null) && (paramString != null)) || ((str != null) && (!(str.equals(paramString)))));
    try
    {
      this.mParser.setURL(paramString);
      this.mPropertySupport.firePropertyChange("URL", str, getURL());
      setLinks();
      return;
    }
    catch (ParserException localParserException)
    {
    }
  }
}